﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Shared.Utils
{
    public static class LinuxPathUtils
    {
        private static char targetSeparator= '/';
        public static string GetDirectoryName(string path)
        {
            var result=Path.GetDirectoryName(path);
            result=result.Replace(Path.DirectorySeparatorChar, targetSeparator)
                               .Replace(Path.AltDirectorySeparatorChar, targetSeparator);
            return result;
        }

        public static string Combine(params string[] paths) {
            var result = Path.Combine(paths);
            result = result.Replace(Path.DirectorySeparatorChar, targetSeparator)
                               .Replace(Path.AltDirectorySeparatorChar, targetSeparator);
            return result;
        }
        /// <summary>
        /// 获取相对路径
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="basePath"></param>
        /// <returns></returns>
        public static string GetRelativePath(string fullPath, string basePath)
        {
            // 确保路径以目录分隔符结尾
            if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                basePath += Path.DirectorySeparatorChar;
            }

            var fullUri = new Uri(fullPath);
            var baseUri = new Uri(basePath);

            // 计算相对路径
            var relativeUri = baseUri.MakeRelativeUri(fullUri);
            var relativePath = Uri.UnescapeDataString(relativeUri.ToString());

            // 替换路径分隔符为Linux分隔符
            return relativePath.Replace(Path.DirectorySeparatorChar, targetSeparator)
                               .Replace(Path.AltDirectorySeparatorChar, targetSeparator);
        }

        public static string GetLinuxDirectoryName(string path)
        {
            path= path.Replace(targetSeparator, Path.DirectorySeparatorChar)
                     .Replace(targetSeparator, Path.AltDirectorySeparatorChar);
            return Path.GetDirectoryName(path).Replace(Path.DirectorySeparatorChar, targetSeparator)
                               .Replace(Path.AltDirectorySeparatorChar, targetSeparator);
        }
    }
}
